package com.event.eventjob.task;

import cn.hutool.core.util.ObjectUtil;
import com.event.cacheclear.service.IClearCacheService;
import com.event.common.constant.CommonConstant;
import com.event.common.constant.LockKeyConstant;
import com.event.eventjob.config.JobConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/*
 * @description 清除缓存任务
 * @author Bing
 * @date 2024/10/8 14:08
 **/
@Component
@ConditionalOnClass(JobConfig.class)
public class ClearCacheTask {

    @Autowired(required = false)
    private IClearCacheService clearCacheService;

    /**
     * 每一分钟执行一次
     */
    @Scheduled(cron = "0 */1 * * * ? ")
    public void execute(){
        if(ObjectUtil.isNull(clearCacheService)){
            return;
        }
        // 获取锁
        boolean flag = clearCacheService.lock(LockKeyConstant.CACHE_CLEAR_LOCK, CommonConstant.EVENT_LOCK_EXPIRE,TimeUnit.SECONDS);
        if(!flag){
           return;
        }
        // 弹出队列清除缓存
        clearCacheService.popQueueClearCache(CommonConstant.EVENT_CACHEJOB_BATCHSIZE);
    }
}
